package com.schoolai.schoolset.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolai.entity.SchoolSetClassInfo;
import com.schoolai.schoolset.auth.service.JwtUser;
import com.schoolai.schoolset.service.ISchoolSetClassInfoService;
import com.schoolai.util.base.Result;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:ClassMNGController
 * Author:bobby
 * 创建时间：2025/9/22 14:50
 * Description:班级管理
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Slf4j
@RequestMapping("/classMNG")
@Tag(name = "班级管理", description = "班级管理接口")
@RestController
public class ClassMNGController {
    @Autowired
    ISchoolSetClassInfoService iSchoolSetClassInfoService;

    @Operation(summary = "班级信息")
    @Parameters({
        @Parameter(name = "id", in = ParameterIn.PATH, description = "班级ID")
    })
    @ApiOperationSupport(order = 1)
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('user:search')")
    Result view(@PathVariable("id") String id){
        if(StringUtils.isBlank(id)) {
            // log.info("参数错误");
            return Result.error("参数错误");
        }
        SecurityContext context = SecurityContextHolder.getContext();//存储认证对象的上下文
        Authentication authentication = context.getAuthentication();//认证对象
        String username = authentication.getName();//用户名
        System.out.println("username:"+username);
        JwtUser principal =(JwtUser)authentication.getPrincipal();
        System.out.println("email:"+principal.getEmail());
        authentication.getAuthorities().forEach(System.out::println);
        return  Result.ok(iSchoolSetClassInfoService.getById(id));
    }
    @Operation(summary = "添加班级信息")
    @PostMapping
    Result save(@RequestBody SchoolSetClassInfo schoolSetClassInfo){
        return Result.ok(iSchoolSetClassInfoService.saveOrUpdate(schoolSetClassInfo));
    }
    @Operation(summary = "修改班级信息")
    @PutMapping
    Result update(@RequestBody SchoolSetClassInfo schoolSetClassInfo){
        return Result.ok(iSchoolSetClassInfoService.updateById(schoolSetClassInfo));
    }
    @Operation(summary = "下线班级信息")
    @DeleteMapping("/{id}")
    Result delete(@PathVariable("id") String id){
        SchoolSetClassInfo classInfo =iSchoolSetClassInfoService.getById(id);
        if(classInfo == null) {
            return Result.error("操作失败！班级不存在");
        }
        classInfo.setDeletedTime(new Date());
        classInfo.setUpdatedTime(new Date());
        return Result.ok(iSchoolSetClassInfoService.updateById(classInfo));
    }
}
